import {createRouter, createWebHistory} from 'vue-router'
import HomeView from '../views/HomeView.vue'

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            name: 'home',
            component: HomeView
        },
        {
            path: '/login',
            name: 'login',
            component: () => import('../views/LoginView.vue')
        }
    ]
})

//路由守卫
router.beforeEach(async (to, from, next) => {
    console.log('to',to)
    console.log('from',from)
    //读取登录用户信息
    let user = JSON.parse(sessionStorage.getItem('user'))
    if(user===null && to.fullPath!=='/login'){
        next('/login')
    }else{
        next()
    }
})

export default router
